// pages/order/index.js
import { request } from '../../request/http.js';
import regeneratorRuntime from '../../lib/runtime/runtime.js';

Page({
  data: {
    tabs: [
      { id: 0, value: '完成', isActive: true },
      { id: 1, value: '待发货', isActive: false },
      { id: 2, value: '待收货', isActive: false },
      { id: 3, value: '退款/退货', isActive: false }
    ],
    istrue:'未发货',
    isfalse:'已发货',
    istuihuo: '',
    istuihuofalse:'退货成功',

    //全部数据
    list0: [],
    // list数据--待发货页面√
    list1:[],
    //待收货
    list2: [],
    // 退款页面数据
    list3:[]
    
  },
  // 标题点击事件, 从子组件转递过来
  handleTabsItemChange(e) {
    const { index } = e.detail;
    let { tabs } = this.data;
    tabs.forEach((v, i) => index == i ? v.isActive = true : v.isActive = false)
    this.setData({ tabs })
  },
  onLoad: function (options) {
    console.log(options);
    const { type } = options;
    let { tabs } = this.data;
    tabs.forEach((v, i) => type == i ? v.isActive = true : v.isActive = false)
    this.setData({ tabs })   
  },
  onShow(){
    this.getdaifahuo();
    this.getdaishouhuo();
    this.gettuikuan();
    this.getallorders();
  },
  onPullDownRefresh: function () {
    this.getdaifahuo();
    this.getdaishouhuo();
    this.gettuikuan();
    this.getallorders();
  },
  //全部订单---0
  async getallorders() {
    const openid = wx.getStorageSync('OPENID');
    const result = await request({ name: 'allorders', data: { openid: openid } });
    // console.log(result.data.message);
    this.setData({
      list0: result.data.message
    })
  },
  //待发货---1
  async getdaifahuo() {
    const openid = wx.getStorageSync('OPENID');
    const result = await request({ name: 'daifahuo', data: { openid: openid } });
    // console.log(result.data.message);
    this.setData({
      list1: result.data.message
    })
  },
  //待收货---2
  async getdaishouhuo() {
    const openid = wx.getStorageSync('OPENID');
    const result = await request({ name: 'daishouhuo', data: { openid: openid } });
    // console.log(result.data.message);
    this.setData({
      list2: result.data.message
    })
  },
  //退款---3
  async gettuikuan() {
    const openid = wx.getStorageSync('OPENID');
    const result = await request({ name: 'tuikuan', data: {openid: openid} });
    // console.log(result.data.message);
    this.setData({
      list3: result.data.message
    })
  },


  //点击退款时请求改变商品状态 传goods_id
  async clicktuikuan(e){
    const openid = wx.getStorageSync('OPENID');
    let goods_id=e.currentTarget.dataset.index;
    const result = await request({ name: 'clicktuikuan', data: { goods_id: goods_id, openid: openid}});
    console.log(result.data.message);
    this.setData({
      list2: result.data.message
    })
    this.gettuikuan();
  },
  //确认收货状态变为4
  async confirmget(e){
    const openid = wx.getStorageSync('OPENID');
    let goods_id = e.currentTarget.dataset.index;
    const result = await request({ name: 'confirmget', data: { goods_id: goods_id,openid:openid } });
    this.setData({
      list2: result.data.message
    })
    this.getallorders();
  },
  //申请退货
  async applyreturngoods(e){
    const openid = wx.getStorageSync('OPENID');
    let goods_id = e.currentTarget.dataset.index;
    const result = await request({ name: 'applyreturngoods', data: { goods_id: goods_id,openid:openid }});
    
    this.setData({ istuihuo: '等待卖家同意',})
  }



})